package main

import (
	"encoding/base64"
	"fmt"
	"gowork/goadmin/services/controllers/captcha"
	"gowork/goadmin/services/controllers/crypto"
	// "gowork/goadmin/services/socket"
	"gowork/goadmin/services/utils"
	"log"
	"os"
	// "gowork/goadmin/services/email"
	// "sync"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cache"
	"github.com/gofiber/fiber/v2/middleware/compress"
	"github.com/gofiber/fiber/v2/middleware/cors"
	"github.com/gofiber/websocket/v2"
	// "github.com/gofiber/fiber/v2/middleware/csrf"
	// "github.com/gofiber/fiber/v2/middleware/limiter"
	// "gorm.io/gorm"
	// "gowork/goadmin/services/controllers/user"
	// "gowork/goadmin/services/orm"
	"time"
)

func handleNoLogin(c *fiber.Ctx) error { //中间件
	return c.Next()
}
func handlerLogin(c *fiber.Ctx) error { //登录中间件
	str := c.Get("Authorization")
	bytestr, err := base64.StdEncoding.DecodeString(str)
	if err != nil {
		return err
	}
	token := string(crypto.RSADecrypt([]byte(bytestr)))
	fmt.Println(token)
	return c.Next()
}

func main() {
	app := fiber.New()
	// app.Use(limiter.New())
	app.Use(compress.New())
	app.Use(cache.New())
	// app.Use(csrf.New()) // Default config
	app.Use(cors.New())

	v1 := app.Group("/v1", handleNoLogin) //不需登录
	v2 := app.Group("/v2", handlerLogin)  //需登录
	v3 := app.Group("/ws")                //socket.io
	v1.Get("/captcha", func(c *fiber.Ctx) error {
		return captcha.Captcha(c)
		// return c.Download("./go.mod")
	})
	v1.Get("/captchaImage/:captchaId", func(c *fiber.Ctx) error {
		return captcha.CaptchaImage(c)
		// return c.Download("./go.mod")
	})
	v3.Get("/:time", websocket.New(func(c *websocket.Conn) {
		fmt.Println("socket")
		for {
			mt, msg, err := c.ReadMessage()
			if err != nil {
				log.Println("read:", err)
				break
			}
			log.Printf("recv: %s", msg)
			err = c.WriteMessage(mt, msg)
			if err != nil {
				panic(err)
			}
			time.AfterFunc(time.Second, func() {
				// JoinMessages(c,"hellow")
			})

		}

	}))
	v2.Post("/file/upload", func(c *fiber.Ctx) error {
		form, err := c.MultipartForm()
		if err != nil {
			return err
		}
		fmt.Println("files")
		files := form.File["files"]
		for _, file := range files {
			// fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
			if util.IsDir("../files/images") {
				if err := c.SaveFile(file, fmt.Sprintf("../files/images/%s", file.Filename)); err != nil {
					return c.SendString("error")
				}
			} else {
				os.MkdirAll("../files/images", os.ModePerm)
				if err := c.SaveFile(file, fmt.Sprintf("../files/images/%s", file.Filename)); err != nil {
					return c.SendString("error")
				}
			}
		}
		return c.SendString("ok")
	})
	log.Fatal(app.Listen(":9999"))

	// var wg sync.WaitGroup
	// mailTo:=[]string{
	// 		"1612346638@qq.com",
	// }
	// subject:="测试勿回"
	// body:="好久不见,这是我的测试邮件"
	// for _,mail:=range mailTo{
	// 		wg.Add(1)
	// 		go email.SendMail(mail,subject,body,"C:/Users/16123/Desktop/工具.txt",&wg)
	// }
	// wg.Wait()

	// db := orm.GetDb()
	// type User struct {
	// 	gorm.Model
	// 	Name     string    `gorm:"type:char(30);comment:'姓名'"`
	// 	Age      int       `gorm:"age"`
	// 	Birthday time.Time `gorm:"birthday"`
	// }
	// db.Migrator().CreateTable(&User{})
	// user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
	// result := db.Create(&user) // 通过数据的指针来创建
	// fmt.Println(result)
}
